package edu.yctc.weblog.controller.rest.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.BlogRestController;
import edu.yctc.weblog.entity.BlogDO;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.BlogService;
import edu.yctc.weblog.utils.PageQueryUtil;
import edu.yctc.weblog.utils.VOUtil;
import edu.yctc.weblog.vo.SaveBlogVO;
import edu.yctc.weblog.vo.UpdateBlogVO;

@RestController
@RequestMapping(URLConstant.S + URLConstant.BLOG)
public class BlogRestControllerImpl implements BlogRestController {

    @Autowired
    private BlogService blogService;

    @Override
    @GetMapping(URLConstant.BLOG_GET_BLOG_LIST)
    public ResultDO<PageDO> getBlogDOList(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }

        return blogService.getBlogPage(new PageQueryUtil(params));
    }

    @Override
    @PostMapping(URLConstant.BLOG_SAVE_BLOG)
    public ResultDO<Void> saveBlog(@RequestBody SaveBlogVO saveBlogVO) {
        // 过筛
        if (StringUtils.isBlank(saveBlogVO.getBlogTitle())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TITLE_UNINPUT);
        }
        if (saveBlogVO.getBlogTitle().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TITLE_OVER_SIZE);
        }
        if (StringUtils.isBlank(saveBlogVO.getBlogTags())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TAG_UNINPUT);
        }
        if (saveBlogVO.getBlogTags().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TAG_OVER_SIZE);
        }
        if (saveBlogVO.getBlogSubUrl().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.PATH_OVER_SIZE);
        }
        if (StringUtils.isBlank(saveBlogVO.getBlogContent())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.CONTENT_UNINPUT);
        }
        if (saveBlogVO.getBlogContent().trim().length() > 100000) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.CONTENT_OVERSIZE);
        }
        if (StringUtils.isBlank(saveBlogVO.getBlogCoverImage())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.COVER_EMPTY);
        }
        BlogDO blogDO = VOUtil.coverSaveBlogVO2BlogDO(saveBlogVO);
        return blogService.saveBlog(blogDO);
    }

    @Override
    @PostMapping(URLConstant.BLOG_UPDATE_BLOG)
    public ResultDO<Void> updateBlog(@RequestBody UpdateBlogVO updateBlogVO) {
        if (StringUtils.isBlank(updateBlogVO.getBlogTitle())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TITLE_UNINPUT);
        }
        if (updateBlogVO.getBlogTitle().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TITLE_OVER_SIZE);
        }
        if (StringUtils.isBlank(updateBlogVO.getBlogTags())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TAG_UNINPUT);
        }
        if (updateBlogVO.getBlogTags().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.TAG_OVER_SIZE);
        }
        if (updateBlogVO.getBlogSubUrl().trim().length() > 150) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.PATH_OVER_SIZE);
        }
        if (StringUtils.isBlank(updateBlogVO.getBlogContent())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.CONTENT_UNINPUT);
        }
        if (updateBlogVO.getBlogContent().trim().length() > 100000) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.CONTENT_OVERSIZE);
        }
        if (StringUtils.isBlank(updateBlogVO.getBlogCoverImage())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.COVER_EMPTY);
        }
        BlogDO blogDO = VOUtil.coverUpdateBlogVO2BlogDO(updateBlogVO);
        return blogService.update(blogDO);
    }

    @Override
    @PostMapping(URLConstant.BLOG_DELETE_BLOG)
    public ResultDO<Void> deleteBlog(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return blogService.deleteBatch(ids);
    }

}
